﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using QLVMBAYDatacess;
using QLVMBAYDomain;

namespace QLVMBAY.Admin
{
    public partial class QLTuyenBay : System.Web.UI.Page
    {
        static bool Insert = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadGrid();
            }
        }
        private void LoadGrid()
        {
            grdTuyenBay.DataSource = TuyenBayDA.Inst.GetAll(true);
            grdTuyenBay.DataBind();
            if (grdTuyenBay.PageCount <= 1)
            {
                grdTuyenBay.PagerStyle.Visible = false;
            }
        }
        void Load_dlSBDi()
        {
            List<SanBay> list = new List<SanBay>();
            list = SanBayDA.Inst.GetAll(true);
            dlSBDi.DataSource = list;
            dlSBDi.DataTextField = "TenSanBay";
            dlSBDi.DataValueField = "MaSanBay";
            dlSBDi.DataBind();
            dlSBDi.Items.Insert(0, new ListItem("Chọn", "0"));
        }
        void Load_dlSBDen()
        {
            List<SanBay> list = new List<SanBay>();
            list = SanBayDA.Inst.GetAll(true);
            dlSBDen.DataSource = list;
            dlSBDen.DataTextField = "TenSanBay";
            dlSBDen.DataValueField = "MaSanBay";
            dlSBDen.DataBind();
            dlSBDen.Items.Insert(0, new ListItem("Chọn", "0"));
        }
        
        protected void AddButton_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = true;
            pnView.Visible = false;            
            Insert = true;
            txtMaTB.Text = "";
            txtTenTB.Text = "";
            Load_dlSBDen();
            Load_dlSBDi();
        }

        protected void RefreshButton_Click(object sender, EventArgs e)
        {
            LoadGrid();
        }

        protected void Update_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                TuyenBay TB = new TuyenBay();
                TB.MaTuyenBay  = txtMaTB.Text;
                TB.TenTuyenBay = txtTenTB.Text;
                TB.ItemSanBayDi = new SanBay() { MaSanBay = dlSBDi.SelectedValue };
                TB.ItemSanBayDen = new SanBay() { MaSanBay = dlSBDen.SelectedValue };
                if (Insert == true)
                {
                    TuyenBayDA.Inst.Insert(TB);
                }
                else
                {
                    TuyenBayDA.Inst.Update(TB);
                }
                LoadGrid();
                pnView.Visible = true;
                pnUpdate.Visible = false;
                Insert = false;
            }
        }

        protected void Back_Click(object sender, EventArgs e)
        {
            pnView.Visible = true;
            pnUpdate.Visible = false;
            Insert = false;
        }

        protected void grdTuyenBay_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            var itemTB = TuyenBayDA.Inst.GetByPrimaryKey(false, e.Item.Cells[0].Text);
            switch (e.CommandName)
            {
                case "Edit":
                    Insert = false;
                    txtMaTB.Text = itemTB.MaTuyenBay.ToString();
                    txtMaTB.ReadOnly = true;
                    txtTenTB.Text = itemTB.TenTuyenBay.ToString();
                    Load_dlSBDi();
                    dlSBDi.SelectedValue = itemTB.ItemSanBayDi.MaSanBay;
                    Load_dlSBDen();
                    dlSBDen.SelectedValue = itemTB.ItemSanBayDen.MaSanBay;
                    pnView.Visible = false;
                    pnUpdate.Visible = true;
                    break;

                case "Delete":
                    TuyenBayDA.Inst.Delete(itemTB);
                    LoadGrid();
                    break;
            }
        }

        protected void grdTuyenBay_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemIndex != -1)
            {
                var SB = (TuyenBay)e.Item.DataItem;
                var lbl1 = e.Item.FindControl("lblTenSBDi") as Label;
                var lbl2 = e.Item.FindControl("lblTenSBDen") as Label;
                lbl1.Text = SB.ItemSanBayDi.TenSanBay;
                lbl2.Text=SB.ItemSanBayDen.TenSanBay;                
            }
        }

        protected void grdTuyenBay_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdTuyenBay.CurrentPageIndex = e.NewPageIndex;
            LoadGrid();
        }
    }
}